1.1 - Code Your Own Adventure

Confirm

You have some programming skills. Time to make something you can show people! We're going to show you how to program a 'code your own adventure' game. It'll have a basic story line, have the user make some choices, and have a happy ending. Then you can modify it as you wish and show off your creative talents!

One note before we begin. Each of the following exercises will ask you to add on to your previous code. Make sure not to delete or change anything that you did in a previous exercise. Good luck!

  1. It's always polite to ask your user if they are ready to play.
  2. Using the confirm command, make sure your user is ready to play. For example, I would use the sentence "I am ready to play!". Add whatever phrase you would like in the confirm.
// Check if the user is ready to play!
confirm("I am ready to play!");
Screen Shot 2016-08-15 at 6.58.31 PM.png
Old enough to play?

This will involve two pieces of code:

  1. First we will use prompt to ask a user for their age, like this:

var age = prompt("What's your age");
  1. The variable age will hold the user's response.

  2. Then we will use an if/else statement based on the age of the user. Here's an outline of the code, similar to what we've seen before:

if(age is less than 13) 
{
    // do this code
}
else   // "otherwise"
{
    // do this code
}
  1. Under the existing code, declare a variable age.
  2. Make age equal to prompt("What's your age");. See the example above.
  3. Then write an if/else statement. Ifage is less than 13, use console.log to tell the user that they're allowed to play but you take no responsibility.
  4. Else, use console.log and give them an encouraging message to play on!
// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if(age < 13){
    console.log("You are allowed to play, but I take no responsibility");
}
else
{
    console.log("Great! Let's start playing");
}
Adding some story

Let's set up the scene for your story. Under all your previous code, print out the following introduction, exactly as it is written.

"You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'"

Print the introduction using console.log. Remember that the introduction is a string, so make sure to keep it between quotes.
// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if(age < 13){
    console.log("You are allowed to play, but I take no responsibility");
}
else
{
    console.log("Great! Let's start playing");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'")
First move!

Next, your user is about to talk to Justin Bieber.

  1. Under your existing code, print out the storyline: "Suddenly, Bieber stops and says, 'Who wants to race me?'"
  2. Then declare a variable userAnswer. Make it equal a prompt that asks the user"Do you want to race Bieber on stage?". This will be the question that you ask your user.
// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if(age < 13){
    console.log("You are allowed to play, but I take no responsibility");
}
else
{
    console.log("Great! Let's start playing");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
The story heats up!

Now you have to create different scenarios. Good thing we know how to do that usingif / else statements.

If userAnswer is "yes", print out: "You and Bieber start racing. It's neck and neck! You win by a shoelace!"

Otherwise, print out: "Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'"

Remember: = is for assignment, and ===is to check if things are equal!

Use an if / else statement to write out the last part of this game!

Make sure to check the console for error messages and suggestions for how to fix them.

// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if(age < 13){
    console.log("You are allowed to play, but I take no responsibility");
}
else
{
    console.log("Great! Let's start playing");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
if (userAnswer == "yes"){
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else
{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
Asking for feedback

It is worthwhile asking your user for feedback!

  1. Create a variable called feedback andprompt the user to rate your game out of 10.

  2. If feedback is greater than 8, print out:"Thank you! We should race at the next concert!"

  3. Otherwise, print out:
    "I'll keep practicing coding and racing."

// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if(age < 13){
    console.log("You are allowed to play, but I take no responsibility");
}
else
{
    console.log("Great! Let's start playing");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
if (userAnswer == "yes"){
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else
{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
var feedback = prompt("Please rate my game");
if (feedback > 8){
    console.log("Thank you! We should race at the next concert!");
}
else
{
    console.log("I'll keep practicing coding and racing.");
}
Next steps!

Congratulations! You have just programmed your first game. That is incredibly impressive.

Hit Save & Submit Code to finish this course!
// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if(age < 13){
    console.log("You are allowed to play, but I take no responsibility");
}
else
{
    console.log("Great! Let's start playing");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
if (userAnswer == "yes"){
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else
{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
var feedback = prompt("Please rate my game");
if (feedback > 8){
    console.log("Thank you! We should race at the next concert!");
}
else
{
    console.log("I'll keep practicing coding and racing.");
}